// page/pack-index/pages/reply-more/reply-more.js
var toolutils = require('../../../../utils/tool-utils.js')
var httpUtils = require('../../../../utils/https-utils.js')
var app = getApp()
var that
var mPageSize = 20
var mTotalNum = 0
var mPuserID
var mPpage//上级页面对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    list:[],
    loadData: {
      searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
      searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
    },
    isReply: false,
    staring:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    var index = options.index
    mPpage = getCurrentPages()[getCurrentPages().length-2]
    var oData = mPpage.data.commentList[index]
    that.setData({
      userName: oData.user_name,
      commentContent: oData.comment_content,
      isStar: oData.is_star,
      branchID: mPpage.data.newsID,
      commentID: oData.comment_id,
      star_num: oData.star_num,
      user_avatar: oData.user_avatar,
      create_time: oData.create_time,
      userPhone: app.data.userPhone
    })
    mPuserID = oData.user_id
    that.getCommentDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //点赞
  zan: function (e) {
    putStar()
  },
  //回复
  reply: function (e) {
    // if (mPuserID == app.data.userID) {
    //   toolutils.showFailToast("自己不能回复自己")
    //   return
    // }
    that.setData({
      isReply: true,
      level: 2,
      commentUserId: mPuserID,
      userName:'',
      isAt:2
    })
  },
  //回复二级
  onItem:function(e){
    var item = e.currentTarget.dataset.item
    // if(item.user_id == app.data.userID){
    //   toolutils.showFailToast("自己不能回复自己")
    //   return
    // }
    that.setData({
      isReply: true,
      commentUserId: item.user_id,
      level: 3,
      userName: item.user_name,
      isAt: 1
    })
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function (e) {
    if (mTotalNum > mPageSize * that.data.currentPage) {
      that.setData({
        currentPage: ++that.data.currentPage,
        loadData: {
          searchLoading: true, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
      getCommentList()
    } else {
      that.setData({
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
        }
      })
    }
  },
  //获取评论列表
  getCommentDetail: function () {
    var params = {}
    params.current_page = that.data.currentPage
    params.page_size = mPageSize
    if (!!app.data.token) {
      params.token = app.data.token
    }
    httpUtils.getCommentDetail(that.data.commentID, params, function (res) {
      mTotalNum = res.total_num
      var list = that.data.list
      if (res.current_page == 1) {
        list = []
      }
      list = list.concat(res.list)
      if (list.length == mTotalNum) {
        that.setData({
          list: list,
          loadData: {
            searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
            searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
          }
        })
      } else {
        that.setData({
          list: list,
          loadData: {
            searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
            searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
          }
        })
      }
    }, function (e) {
      toolutils.showFailToast("获取评论列表失败")
      that.setData({
        currentPage: --that.data.currentPage,
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
    })
  }
})
//评论点赞
function putStar() {
  var params = {}
  if (!app.data.token) {
    toolutils.showFailToast("token为空")
    return
  }
  params.token = app.data.token
  params.star_type = 2//评论点赞
  params.branch_id = that.data.commentID
  //点下禁用按钮和数量加一
  that.setData({
    staring: true,
    star_num: that.data.isStar == 1 ? ++that.data.star_num : --that.data.star_num,
    isStar: that.data.isStar == 1 ? 2 : 1
  })
  httpUtils.putStar(params, function (res) {
    toolutils.showFailToast(that.data.isStar == 2 ? "点赞成功" : "取消点赞成功")
    that.setData({
      staring: false
    })
    if(!mPpage.data.refresh){
      mPpage.setData({
        currentPage:1,
        refresh:true
      })
    }
  }, function (e) {
    toolutils.showFailToast(that.data.isStar == 2 ? "点赞失败" : "取消点赞失败")
    that.setData({
      staring: false,
      star_num: that.data.isStar == 2 ? --that.data.star_num : ++that.data.star_num,
      isStar: that.data.isStar == 2 ? 1 : 2
    })
  })
}

